package com.shushan.home.adapter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.shushan.home.bean.LearnByDoBean;
import com.shushan.shushanbao.R;
import com.shushan.util.ImageUtils;

import java.util.List;

/**
 * @ClassName: SubjectRecyclerViewAdapter
 * @Desciption: //各科目下的RecyclerView适配器
 * @author: zhangshihao
 * @date: 2018-04-08
 */
public class SubjectRecyclerViewAdapter extends RecyclerView.Adapter<SubjectRecyclerViewAdapter.MyViewHolder> {

    private Context mContext;
    private OnItemClickListener listener;
    private List<LearnByDoBean> learnByDoList;

    public SubjectRecyclerViewAdapter(Context context,List<LearnByDoBean> learnByDoList){
        this.mContext = context;
        this.learnByDoList = learnByDoList;
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener = listener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(mContext).inflate(
                R.layout.item_subject_recycler_view,null,false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        LearnByDoBean bean = learnByDoList.get(position);
        ImageUtils.loadImageView(mContext,bean.cover,holder.ivSpecialTopicPic);
        holder.tvSpecialTopicTitle.setText(bean.video_title);
        holder.tvSpecialTopicDes.setText(bean.video_desc);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    int position = holder.getLayoutPosition();
                    listener.onItemClick(holder.itemView,position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return learnByDoList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        private ImageView ivSpecialTopicPic;
        private TextView tvSpecialTopicTitle;
        private TextView tvSpecialTopicDes;

        public MyViewHolder(View itemView) {
            super(itemView);
            ivSpecialTopicPic = (ImageView) itemView.findViewById(R.id.iv_special_topic_pic);
            tvSpecialTopicTitle = (TextView) itemView.findViewById(R.id.tv_special_topic_title);
            tvSpecialTopicDes = (TextView) itemView.findViewById(R.id.tv_special_topic_des);
        }
    }

   public interface OnItemClickListener{
        void onItemClick(View itemView, int position);
    }

    public List<LearnByDoBean> getLearnByDoList() {
        return learnByDoList;
    }

    public void setLearnByDoList(List<LearnByDoBean> learnByDoList) {
        this.learnByDoList = learnByDoList;
    }
}
